package com.sjk.service.impl;

import com.sjk.pojo.User;
import com.sjk.repository.LoginDao;
import com.sjk.service.LoginService;
import com.sjk.utils.EncryptAlgorithm;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author lohhhha
 * 2023/5/17
 */

@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    LoginDao loginDao;

    @Override
    public User login(String username, String password) {
        User u = loginDao.findByUsernameAndPassword(username, EncryptAlgorithm.encrypt(password));
        if (u != null) {
            u.setPassword("");
        }
        return u;
    }

    /**
     * @return false:存在重复username true:正常
     */
    @Override
    public boolean register(User u) {
        User findRes = loginDao.findByUsername(u.getUsername());
        if (findRes == null) {
            loginDao.save(u);
            return true;
        }
        return false;
    }
}
